﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ESRI.ArcGIS.Client;

namespace Eaglight
{
    public partial class Toc : UserControl
    {
        private List<TreeViewItem> treeItems;
        public Toc()
        {
            InitializeComponent();
            treeItems = new List<TreeViewItem>();
        }

        private static void OnMapChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            Toc toc = (Toc)d;
            toc.initLayers();
        }

        private TreeViewItem getTreeViewItem(DependencyObject o)
        {
            DependencyObject parent = o;
            do
            {
                parent = VisualTreeHelper.GetParent(parent);
            } while (parent != null && parent.GetType() != typeof(TreeViewItem));
            return parent as TreeViewItem;
        }

        private void InitDynamicMapServiceLayerDefaultVisibleLayers(ArcGISDynamicMapServiceLayer mapServiceLayer)
        {
            List<int> visibleLayers = new List<int>();

            foreach (LayerInfo layerInfo in mapServiceLayer.Layers)
            {
                if (layerInfo.DefaultVisibility)
                {
                    visibleLayers.Add(layerInfo.ID);
                }
            }
            mapServiceLayer.VisibleLayers = visibleLayers.ToArray();
        }

        private void initLayers()
        {
            treeItems.Clear();
            foreach (Layer lyr in Map.Layers)
            {
                TreeViewItem tvi = new TreeViewItem();
                tvi.Header = lyr;
                tvi.HeaderTemplate = MapLayersTemplate;
                treeItems.Add(tvi);
            }
            tvMain.ItemsSource = treeItems;

            foreach (TreeViewItem tvi in tvMain.Items)
            {
                Layer lyr = tvi.Header as Layer;
                if (lyr is ArcGISDynamicMapServiceLayer)
                {
                    ArcGISDynamicMapServiceLayer gLyr = lyr as ArcGISDynamicMapServiceLayer;
                    gLyr.Initialized += new EventHandler<EventArgs>(gLyr_Initialized);
                    gLyr.InitializationFailed += new EventHandler<EventArgs>(gLyr_InitializationFailed);
                }
            }
        }

        void gLyr_InitializationFailed(object sender, EventArgs e)
        {
            ArcGISDynamicMapServiceLayer dl = (ArcGISDynamicMapServiceLayer)sender;
            TreeViewItem tvi = (TreeViewItem)(treeItems.Single(t => t.Header == dl));
            TextBlock tb = new TextBlock();
            tb.Text = "Initialization Failed";
            tb.Foreground = new SolidColorBrush(Colors.Red);
            tvi.Items.Add(tb);
        }

        private void initDynamicLayerItems(ArcGISDynamicMapServiceLayer dl)
        {
            if (dl.Layers != null)
            {
                InitDynamicMapServiceLayerDefaultVisibleLayers(dl);
                TreeViewItem tvi = (TreeViewItem)(treeItems.Single(t => t.Header == dl));
                tvi.Items.Clear();
                tvi.ItemTemplate = DynamicLayerTemplate;
                foreach (LayerInfo li in dl.Layers)
                {
                    tvi.Items.Add(li);
                }
            }
        }

        void gLyr_Initialized(object sender, EventArgs e)
        {
            ArcGISDynamicMapServiceLayer dl = (ArcGISDynamicMapServiceLayer)sender;
            initDynamicLayerItems(dl);
        }

        public static readonly DependencyProperty MapProperty = DependencyProperty.Register("Map", typeof(Map), typeof(Toc),
            new PropertyMetadata(new PropertyChangedCallback(OnMapChanged)));

        public Map Map
        {
            get
            {
                return (Map)GetValue(MapProperty);
            }
            set
            {
                SetValue(MapProperty, value);
            }
        }

        public void Init()
        {
            initLayers();
        }

        private void chkVisible_Checked(object sender, RoutedEventArgs e)
        {
            CheckBox chkBox = (CheckBox)sender;
            var a = tvMain.ItemContainerGenerator.ContainerFromItem(chkBox);
            TreeViewItem tvi1 = getTreeViewItem(chkBox);
            TreeViewItem tvi2 = getTreeViewItem(tvi1);
            ArcGISDynamicMapServiceLayer dl = (ArcGISDynamicMapServiceLayer)tvi2.Header;

            if (chkBox.Tag != null)
            {
                int lid = (int)chkBox.Tag;
                List<int> visibleLayers = dl.VisibleLayers.ToList();
                if (!visibleLayers.Contains(lid))
                {
                   visibleLayers.Add(lid);
                }
                dl.VisibleLayers = visibleLayers.ToArray();
            }
        }

        private void chkVisible_Unchecked(object sender, RoutedEventArgs e)
        {
            CheckBox chkBox = (CheckBox)sender;
            var a = tvMain.ItemContainerGenerator.ContainerFromItem(chkBox);
            TreeViewItem tvi1 = getTreeViewItem(chkBox);
            TreeViewItem tvi2 = getTreeViewItem(tvi1);
            ArcGISDynamicMapServiceLayer dl = (ArcGISDynamicMapServiceLayer)tvi2.Header;

            if (chkBox.Tag != null)
            {
                int lid = (int)chkBox.Tag;
                List<int> visibleLayers = dl.VisibleLayers.ToList();
                if (visibleLayers.Contains(lid))
                {
                    visibleLayers.Remove(lid);
                }
                dl.VisibleLayers = visibleLayers.ToArray();
            }
        }
    }
}
